var root_path = js_base_url; //alert(root_path);
var upload_error = Drupal.t("Please upload the attachment before you submit Initiation Form.");
//phase4 get url parameter function
function geturlparameter(paras){ 
    var url = location.href; 
    var paraString = url.substring(url.indexOf("?")+1,url.length).split("&"); 
    var paraObj = {} 
    for (i=0; j=paraString[i]; i++){ 
    paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length); 
    } 
    var returnValue = paraObj[paras.toLowerCase()]; 
    if(typeof(returnValue)=="undefined"){ 
    return ""; 
    }else{ 
    return returnValue; 
    } 
}
    

function set_mandatory(box_id,input_id,flag){
    var box_obj = jQuery('#'+box_id);
    var input_obj = jQuery('#'+input_id);
    if(flag == 0){//non-mandatory
        box_obj.find("label .form-required").hide();
        box_obj.find("label.error").hide();
        input_obj.removeClass("required");
        input_obj.removeClass("error");
    }else{//mandatory
        box_obj.find("label .form-required").show();
        input_obj.addClass("required");   
    }
}

function set_display(box_id,input_id,flag){
    var box_obj = jQuery('#'+box_id);
    var input_obj = jQuery('#'+input_id);
    if(flag == 0){//hidden
       box_obj.fadeOut(0);
       box_obj.find("label.error").hide();
       input_obj.removeClass("error");
       input_obj.val('');
    }else{//show
        box_obj.fadeIn(0);
    }
}

//Validate set start
jQuery.extend(jQuery.validator.messages,{
    required: Drupal.t("This field is required."),
    email: Drupal.t("Please enter a valid email address."),
    url: Drupal.t("Please enter a valid URL."),
    date: Drupal.t("Please enter a valid date."),
    number: Drupal.t("Please enter a valid number."),
    digits: Drupal.t("Please enter only digits."),
    creditcard: Drupal.t("Please enter a valid credit card number."),
    equalTo: Drupal.t("Please enter the same value again."),
    maxlength: jQuery.validator.format(Drupal.t("Please enter no more than {0} characters.")),
    minlength: jQuery.validator.format(Drupal.t("Please enter at least {0} characters.")),
    rangelength: jQuery.validator.format(Drupal.t("Please enter a value between {0} and {1} characters long.")),
    range: jQuery.validator.format(Drupal.t("Please enter a value between {0} and {1}.")),
    max: jQuery.validator.format(Drupal.t("Please enter a value less than or equal to {0}.")),
    min: jQuery.validator.format(Drupal.t("Please enter a value greater than or equal to {0}.")),

    costcentre: Drupal.t("Allow up to 12 alphanumeric characters."),
    default_date: Drupal.t("It's not a valid date."),
    //required_upload: Drupal.t("Please upload the attachment before you submit Initiation Form."),
    no_number: Drupal.t("Number is not allowed."),
    english: Drupal.t("English is required."),
    english_comment: Drupal.t("English is required.")
});

jQuery.validator.addMethod("costcentre", function(value, element) {   
    return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);   
});
jQuery.validator.addMethod("no_number", function(value, element) {   
    return this.optional(element) || !/\d/.test(value);   
});
jQuery.validator.addMethod("english", function(value, element) {   
    return this.optional(element) || /^[a-zA-Z\'|\s]+$/.test(value);   
});
jQuery.validator.addMethod("english_comment", function(value, element) {   
    //var reg = new RegExp("^[\\w\\s,-:;!@#$%&*()<>=+\.\'\"\?]*$");
    var reg = new RegExp("^[\\w\\s!@#$%&*()_=+\-\^{}~`\|:;\'\",<>\.\?]*$");
    //var reg = new RegExp(/^[a-z0-9_\-,\. \n#%&*\(\)\?!\']+$/gi);
    return this.optional(element) || reg.test(value); 
});

//End validate set


jQuery(document).ready(function() {
    //-----------------------Fix the IE 'readonly' return bug-------------------------------
    jQuery('input[readonly]').keydown(function(e) {
        //e.preventDefault();
        if(e.keyCode==8){  
            return false; 
        }
    });
    jQuery('input[readonly]').each(function(){
        jQuery(this).addClass("readonly");
    });
    jQuery('textarea[readonly]').keydown(function(e) {
        //e.preventDefault();
        if(e.keyCode==8){  
            return false; 
        }
    });
    jQuery('textarea[readonly]').each(function(){
        jQuery(this).addClass("readonly");
    });
    //--------------------------------------End----------------------------------------------
    jQuery("input.webform-calendar").bind("blur",function(){
        var that = jQuery(this),
        pum = 0;
        jQuery(this).parents("div:first").find("select").each(function(){
            var This = jQuery(this);
            setTimeout(function(){
                if(This.val() == ""){
                    return
                }else{
                    This.removeClass("error");
                    pum = pum + 1;
                    if(pum == 3){
                        that.parents("div:first").find("label.error").hide();
                    }
                }
            },500);
        });
    });
});